﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ELMWCFServiceClass.ELMWCF;
using ELMApplication.Common.Const;
using ELMApplication.Common.DataResource;
using ELMApplication.Common.Util;

namespace SettingModule.Views.ELMItemsView
{
    public partial class CW_ELMItemDetail : ChildWindow
    {
        public CW_ELMItemDetail(IELMItemsPrentationModel model)
        {
            InitializeComponent();
            this.DataContext = this.model = model;
            this.Loaded += new RoutedEventHandler(CW_User_Loaded);
        }
        //initial data
        void CW_User_Loaded(object sender, RoutedEventArgs e)
        {
          
            extInfo.ClearContent();
            
            switch (model.DetailPrivege)
            {
                case PrivegeTypes.New:
                    extInfo.Load(model.CurrentElmList.ContentType.Fields);
                    break;
                case PrivegeTypes.Edit:

                    extInfo.Load(model.CurrentElmItem.Datas);
                    // cbDuty.UpdateLayout();
                    break;
            }
        }
        private IELMItemsPrentationModel model;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            model.CurrentElmItem.Datas = extInfo.GetFields();
            switch (model.DetailPrivege)
            {
                case PrivegeTypes.New:

                    BusyMessage.Show();
                    //如果存在Image类型，并有值
                    bool isNeedUploadFileFirst = false;
                    int cacheCount = extInfo.FilesCache.Count;
                    if (cacheCount > 0)
                    {
                        isNeedUploadFileFirst = true;
                    }
                    foreach (var item in model.CurrentElmItem.Datas)
                    {

                        if (item.Key.FieldType == FieldTypes.Image.ToString() && !string.IsNullOrEmpty(item.Value))
                        {
                            Guid key = new Guid(item.Value);
                            if (extInfo.FilesCache.Any(c => c.Key == key))
                            {

                                byte[] datas = extInfo.FilesCache[key];
                                UploadDataResource udr = new UploadDataResource(key, datas, model.CurrentElmItem.ELMItemId.ToString() + ".jpg");
                                udr.Upload();
                                cacheCount--;
                                udr.UploadedCompled += (sender1, e1) =>
                                {
                                    if (cacheCount == 0)
                                    {
                                        model.SaveData();
                                    };
                                };
                            }
                        }
                    }
                    if (!isNeedUploadFileFirst)
                    {
                        model.SaveData();
                    }
                    break;
                case PrivegeTypes.Edit:
                    BusyMessage.Show();
                    //如果存在Image类型，并有值
                     isNeedUploadFileFirst = false;
                     cacheCount = extInfo.FilesCache.Count;
                    if (cacheCount > 0)
                    {
                        isNeedUploadFileFirst = true;
                    }
                    foreach(var item in model.CurrentElmItem.Datas)
                    {                 
                        
                        if(item.Key.FieldType==FieldTypes.Image.ToString()&&!string.IsNullOrEmpty(item.Value))
                        {
                            Guid key=new Guid(item.Value);
                           if(extInfo.FilesCache.Any(c=>c.Key==key))
                           {
                               
                               byte[] datas=extInfo.FilesCache[key];
                               UploadDataResource udr = new UploadDataResource(key, datas, model.CurrentElmItem.ELMItemId.ToString()+".jpg");
                               udr.Upload();
                               cacheCount--;
                               udr.UploadedCompled += (sender1, e1) =>
                                   {
                                       if (cacheCount == 0)
                                       {
                                           model.SaveData();
                                       };
                                   };
                           }
                        }
                    }
                    if (!isNeedUploadFileFirst)
                    {
                        model.SaveData();
                    }
                    
                    
                    break;
            }
            this.DialogResult = true;
        }



        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
      
    }
}

